// pages/reserve/index.js
const app = getApp()
import http from '../../utils/request.js';
import alert from '../../utils/alert.js';
import { $wuxCalendar } from '../../wux/index'
import { $wuxSelect } from '../../wux/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:'',
    calendar: [],
    eatType:'',
    eatTypeName:'',
    tablePopup:false,
    buttons: [{
      type: 'energized',
      block: true,
      text: '联系店内',
    }],
    eatOptions:[],
    resultIcon: {
      type: 'info',
    },
    is_reservation:"0",
    tables:[],
    tableData:{},
    scrollHeight:"500px"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var res = wx.getSystemInfoSync();
    this.setData({
      phone:options.phone,
      scrollHeight: res.windowHeight -97 + 'px'
    })
    this.getEatType();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  callPhone: function (e) {
    wx.makePhoneCall({ phoneNumber: this.data.phone });
  },
  openCalendar() {
    var that =this;
    $wuxCalendar().open({
      value: that.data.calendar,
      minDate: that.data.eatDay.minDate,
      maxDate: that.data.eatDay.maxDate,
      onChange: (values, displayValues) => {
        that.setData({
          calendar: displayValues,
        })
        that.getEatType(displayValues[0], that.data.eatType);

      },
    })
  },
  onEatType() {
    $wuxSelect('#wux-select').open({
      value: this.data.eatType,
      options: this.data.eatOptions,
      onConfirm: (value, index, options) => {
        if (index !== -1) {
          this.setData({
            eatType: value,
            eatTypeTitle: options[index].title,
          })
          that.getEatType(that.data.calendar[0], value);
        }
      },
    })
  },
  getEatType(dinnertime,eat_type){
    http.post(app.config.service.getReserveInit, {
      dinnertime: dinnertime,
      eat_type: eat_type
    }).then(data => {
      this.setData({
        is_reservation: data.is_reservation,
        eatOptions:data.time,
        eatType: data.time_value,
        eatTypeTitle: data.time[data.time_value].title,
        calendar: [data.date.today],
        eatDay: data.date,
        tables:data.tables
      })
    }, err => {
      alert.alert(err)
    });
  },
  navClick:function(e){
    this.setData({
      toNavView: e.currentTarget.id,
      toTableView: e.currentTarget.id,
      navActive: e.currentTarget.dataset.id
    })
  },
  goInfo:function(e){
    wx.navigateTo({
      url: './info/index',
    })
  },
  goReserve:function(e){
    var table = e.currentTarget.dataset.table;
    this.setData({
      tablePopup:true,
      tableData:{
        day: this.data.calendar[0],
        eat_type:this.data.eatType,
        eat_type_name:this.data.eatTypeTitle,
        table_id: table.id,
        table_name: table.name,
        deposit_fee: table.deposit_fee
      }
    })

  },
  tableSubmit:function(e){
    this.setData({
      tablePopup: false,
    })

    var params=Object.assign(this.data.tableData,{pay_type:'wechat',pay_channel:'miniapp',username:e.detail.value.username,mobile:e.detail.value.mobile});

    http.post(app.config.service.submitReserve, params).then(data => {
      if(data){
        wx.requestPayment({
          timeStamp: data.timeStamp,
          nonceStr: data.nonceStr,
          package: data.package,
          signType: data.signType,
          paySign: data.paySign,
          success: function (res) {
            //支付成功调用查询接口
            http.get(app.config.service.queryReserve,{
              order_no: data.aux_order
            }).then(data=>{
              wx.showToast({
                title: '支付成功',
              })
              }, err => {
                alert.alert(err)
            })
          },
          error: function (res) {
            alert.alert(res)
          }
        })
      }else{
        wx.showToast({
          title: '已经添加成功,请等店内确认',
        })
      }
    }, err => {
      alert.alert(err)
    });
  },
  onCloseTablePopup:function(){
    this.setData({
      tablePopup: false,
    })
  }
})